package com.songtech.basedao.controller;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.basedao.model.BaseDaoTest2;
import com.songtech.basedao.service.BaseServiceImpl2;

/**
 * 测试dao基本功能
 * <br>使用方式二、
 * <br>分别使用基本数据访问的dao和自己的dao
 * <br>service中注入格式：@Resource(name = "com.songtech.basedao.model.BaseDaoTest1")，
 * <br>com.songtech.basedao.model.BaseDaoTest1是要注入的dao将会处理的entity对象
 */
@Controller
@RequestMapping("/basedao2")
public class BaseDaoController2 {
	@Autowired
	private BaseServiceImpl2 serviceDaoImpl;
	
	/**
	 * 根据id查询数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectOne/{id}")
	@ResponseBody
	public JsonResult selectOne(@PathVariable("id") String id){
		BaseDaoTest2 bdt2 = null;
		try {
			bdt2 = serviceDaoImpl.get(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(bdt2, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		return new JsonResult(bdt2, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 查询所有的数据
	 * @return
	 */
	@RequestMapping("/selectAll")
	@ResponseBody
	public JsonResult selectAll(){
		List<BaseDaoTest2> list = serviceDaoImpl.getAll();
		return new JsonResult(list, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
}
